﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Web;
using log4net;

namespace Utils
{
	/// <summary>
	/// class for getting a response from a url
	/// </summary>
	public static class WebReq
	{
		private static ILog logger = LogManager.GetLogger(typeof(WebReq));
		// fore use if using proxy for requests
		const string PROXY_ADDRESS = "132.227.62.25";
		const int PROXY_PORT = 3124;


		public static string GetWebResponse(string url, CookieContainer cookies, string postData)
		{
			return GetWebResponse(url, 300000, false, cookies, null, postData);
		}


		/// <summary>
		/// for receiving the text response
		/// </summary>
		/// <param name="url"></param>
		/// <param name="timeOut"></param>
		/// <param name="useProxy"></param>
		/// <returns></returns>
		public static string GetWebResponse(string url, int timeOut, bool useProxy, CookieContainer cookies, Dictionary<string, string> headers, string postData)
		{
			var resp = GetHttpWebResponse(url, timeOut, useProxy, cookies, headers, postData);
			return GetResponseText(resp);
		}

		public static string GetWebResponse(string url)
		{
			return GetWebResponse(url, 30000, false, null, null, null);
		}


		public static string GetWebResponse(string url, int timeOut, bool useProxy)
		{
			if (useProxy)
			{
				WebProxy webProxy = new WebProxy(PROXY_ADDRESS, PROXY_PORT);

				// Disable Proxy use when the host is local i.e. without periods.
				webProxy.BypassProxyOnLocal = true;

				// Now actually take over the global with our new settings, all new requests
				// use this proxy info
				HttpWebRequest.DefaultWebProxy = webProxy;
			}

			HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
			// this is needed for some sites to allow us in
			req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";

			req.Method = "GET";
			req.Timeout = timeOut;

			HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

			StreamReader sr = new StreamReader(resp.GetResponseStream());

			string resText = sr.ReadToEnd();

			resp.Close();
			sr.Close();

			return resText;
		}

		/// <summary>
		/// for receiving a webresponse object
		/// </summary>
		/// <param name="url"></param>
		/// <param name="timeOut"></param>
		/// <param name="useProxy"></param>
		/// <returns></returns>
		public static HttpWebResponse GetHttpWebResponse(string url, int timeOut, bool useProxy, CookieContainer cookieContainer, Dictionary<string, string> headers, string postData)
		{
			if (useProxy)
			{
				WebProxy webProxy = new WebProxy(PROXY_ADDRESS, PROXY_PORT);

				// Disable Proxy use when the host is local i.e. without periods.
				webProxy.BypassProxyOnLocal = true;

				// Now actually take over the global with our new settings, all new requests
				// use this proxy info
				HttpWebRequest.DefaultWebProxy = webProxy;
			}

			HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
			// this is needed for some sites to allow us in
			req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";

			req.Method = "GET";
			req.Timeout = timeOut;
			req.AllowAutoRedirect = true;
			if (headers != null)
				foreach (var item in headers)
					req.Headers.Add(item.Key, item.Value);

			req.CookieContainer = cookieContainer;
			if (cookieContainer != null)
				req.CookieContainer = cookieContainer;

			if (!string.IsNullOrEmpty(postData))
			{
				ASCIIEncoding encoding = new ASCIIEncoding();
				byte[] data = encoding.GetBytes(postData);

				req.ContentType = "application/x-www-form-urlencoded";
				req.ContentLength = data.Length;
				req.Method = "POST";
				Stream sw = req.GetRequestStream();

				sw.Write(data, 0, data.Length);
				sw.Flush();
				sw.Close();
			}

			HttpWebResponse resp = null;
			try
			{
				resp = req.GetResponse() as HttpWebResponse;

			}
			catch (Exception ex)
			{
				logger.Debug(ex);
			}

			return resp;
		}

		public static string GetResponseText(HttpWebResponse res)
		{
			string resText;
			try
			{
				StreamReader sr = new StreamReader(res.GetResponseStream());

				resText = sr.ReadToEnd();

				sr.Close();

			}
			finally
			{
				res.Close();

			}
			return resText;
		}
	}
}
